// import TextureSliceRender from "../../TextureSlice/TextureSliceRender";
// import { CommonUtil } from "../Util/CommonUtil";
// import AssemblerBase from "./AssemblerBase";

import AssemblerBase from "../Common/Assemblers/AssemblerBase";
import { CommonUtil } from "../Common/Util/CommonUtil";
import TextureSliceRender from "./TextureSliceRender";

const gfx = cc['gfx'];

let vfmtPosUvColor = new gfx.VertexFormat([
    // 节点的世界坐标，占2个float32
    { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },

    // 节点的纹理uv坐标，占2个float32
    // 如果节点使用了独立的纹理（未合图），这里的uv值通常是0或1
    // 合图后的纹理，这里的uv对应其在图集里的相对位置，取值范围在[0,1)内
    { name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 },

    // 节点颜色值，占4个uint8 = 1个float32
    { name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
]);

export default class TextureSliceAssembler extends AssemblerBase {

    public updateRenderData(comp: TextureSliceRender) {
        if (comp._vertsDirty) {
            this.updateUVs(comp);
            this.updateVerts(comp);
            this.updateColor(comp, null);
            comp._vertsDirty = false;
        }
    }

    /**
     * @override
     * @dec 初始化 `_renderData` 数据
     */
    init(comp: any) {
        super.init(comp);

        this._renderData = new cc.RenderData();
        this._renderData.init(this);
        this.initData();
    }

    getVfmt() {
        return vfmtPosUvColor;
    }

    initQuadIndices(indices: number[], arr: number[]) {
        for (let i = 0; i < arr.length; i++) {
            indices[i] = arr[i];
        }
    }

    updateVerts(comp: TextureSliceRender) {
        let indeicesArr = CommonUtil.splitPolygon(comp.polygon);
        this.initQuadIndices(this._renderData.iDatas[0], indeicesArr);
        
        let verts = this._renderData.vDatas[0];
        let floatsPerVert = this.floatsPerVert;
        let polygon = comp.polygon;
        for (let i = 0; i < polygon.length; i++) {
            verts[i * floatsPerVert] = polygon[i].x;
            verts[i * floatsPerVert + 1] = polygon[i].y;
        }
    }

    updateUVs(comp: TextureSliceRender) {
        let uvOffset = this.uvOffset;
        let floatsPerVert = this.floatsPerVert;
        let verts = this._renderData.vDatas[0];
        let uvs = [];
        if (comp.texture) {
            uvs = CommonUtil.computeUv(comp.polygon, comp.texture.width, comp.texture.height)
        }
        let polygon = comp.polygon;
        for (let i = 0; i < polygon.length; i++) {
            let dstOffset = floatsPerVert * i + uvOffset;
            verts[dstOffset] = uvs[i].x;
            verts[dstOffset + 1] = uvs[i].y;
        }
    }

    updateColor(comp: TextureSliceRender, color: number) {
        let uintVerts = this._renderData.uintVDatas[0];
        if (!uintVerts) return;
        color = color != null ? color : comp.node.color['_val'];
        let floatsPerVert = this.floatsPerVert;
        let colorOffset = this.colorOffset;

        let polygon = comp.polygon;
        for (let i = 0; i < polygon.length; i++) {
            uintVerts[colorOffset + i * floatsPerVert] = color;
        }
    }
}